<?php


namespace app\api\controller;


use app\common\service\DidService;
use think\Request;
use think\response\Json;

class DidController extends ApiBaseController
{
    /**上传图片
     * @param Request $request
     * @param DidService $service
     * @return Json
     */
    public function addImage(Request $request,DidService $service): Json{
        try {
            $image = $request->file('image');
            if (empty($image) || empty($image->getOriginalName())){
                throw new \Exception('参数错误，上传图片不能为空。');
            }

            //图片验证
            $config = config('filesystem.disks.public.validate.image');

            validate(['image' => $config])->check(['image'=>$image]);

            return api_success($service->addImage($image,$this->user));
        }catch (\Exception $e){
            return api_error($e->getMessage());
        }
    }

    public function images(DidService $service): Json{

        try {
            $this->param['user_id'] = $this->user->id;
            return api_success($service->getImages($this->param,$this->page,$this->limit));
        }catch (\Exception $e){
            return api_error($e->getMessage());
        }
    }

    /**
     * @param DidService $service
     * @return Json
     */
    public function delImage(DidService $service): Json{

        try {
            $this->param['user_id'] = $this->user->id;
            if ($service->delImage($this->param)){
                return api_success();
            }else{
                throw new \Exception('删除失败！');
            }
        }catch (\Exception $e){
            return api_error($e->getMessage());
        }
    }

    /**
     * @param DidService $service
     * @return Json
     */
    public function delTalk(DidService $service): Json{

        try {
            $this->param['user_id'] = $this->user->id;
            if ($service->delTalk($this->param)){
                return api_success();
            }else{
                throw new \Exception('删除失败！');
            }
        }catch (\Exception $e){
            return api_error($e->getMessage());
        }
    }
    /**
     * 创建演讲
     * @param DidService $service
     * @return Json
     */
    public function addTalk(DidService $service): Json{

        try {
            if (empty($this->param['source_url'])){
                throw new \Exception('参数错误，source_url不能为空。');
            }
            if (empty($this->param['text'])){
                throw new \Exception('参数错误，text不能为空。');
            }
            $this->param['user_id'] = $this->user->id;

            return api_success($service->addTalk($this->param),true);
        }catch (\Exception $e){
            return api_error($e->getMessage());
        }
    }

    public function talk(DidService $service){

        try {
            $data = $service->getTalkById($this->param['id']);
            if (empty($data) || $data['user_id'] != $this->user->id){
                throw  new \Exception('数据不存在！');
            }
            return api_success($data);
        }catch (\Exception $e){
            return api_error($e->getMessage());
        }
    }

    /**
     * 获取演讲列表
     * @param DidService $service
     * @return Json
     */
    public function talks(DidService $service): Json{

        try {
            $this->param['user_id'] = $this->user->id;
            return api_success($service->getTalks($this->param,$this->page,$this->limit));
        }catch (\Exception $e){
            return api_error($e->getMessage());
        }
    }
}